import {
	defineStore
} from 'pinia'
import {
	AuthApi
} from '../../api/login'
export const useAuthStore = defineStore('useAuthStore', {
	state: () => ({
		token: '',
		authInfo: null,
		userData: null,
		// authInfo: {
		// 	opendid: 'orQkb6rH3eKTJEOj9Sn-xJ1inMD4',
		// },
		// token: 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImY3NjI0OTdjLWNjN2EtNGUxMS05YzM4LTE1OWUzYzA2OWUwYSJ9.L3x37czRzUCTBqKC99eKivQRYirSiZ_RMtTHGa8Wg1b8iGWy66ykHt1GVBzUfKify5sUCKuJbX0nfGldiGBIzg',
		// userData: {
		// 	userId: 1,
		// 	deptId: 100,
		// },
	}),
	actions: {
		setToken(data) {
			this.token = data
			this.getUserInfo()
		},
		clearToken() {
			this.token = ''
		},
		setAuthInfo(data) {
			this.authInfo = data
		},
		getUserInfo: async function() {
			const res = await AuthApi.getLoginUserInfo()
			console.log(res);
			this.userData = res.data
		}
	},
	getters: {
		isLogin() {
			return (this.userId && this.token) || false
		},
		userInfo(state) {
			return state.userData?.user || null
		},
		userId() {
			return this.userInfo?.userId
		},
		deptId() {
			return this.userInfo?.deptId
		},
		userPermission(state) {
			return state.userData?.permissions || null
		},
		teams(state) {
			return state.userData.teams || []
		}
	},
	persist: true,
})